﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Ionic.Zip;
using System.Diagnostics;
using System.Threading;


namespace EITSurferInfinityLib
{
    public class oeit
    {
        FramesWriter fw;
        FramesReader fr;
        LogsWR log;
        string TempPath;
        public string DirPath;
        string Name;
        string Extension;
        public int ConfigPointer;
        string SavedDirPath;

        public oeit(string name)
        {
            Setup(name);
        }
        public oeit()
        {
            string name = DateTime.UtcNow.ToString();
            name=name.Replace(':', ' ');
            Setup(name);
        }

        private void Setup(string name)
        {
            fw = new FramesWriter(".rframes",1000,null);
            fr = new FramesReader();
            TempPath = "temp\\";
            DirPath = null;
            CreateTempfolder(name);
            Extension = ".oeit";
            ConfigPointer = -1;
        }

        private void CreateTempfolder(string name)
        {
            Name = name;
            DirPath = TempPath + name;
            fw.DirPath = DirPath + "\\raw\\data";
            fr.DirPath = DirPath + "\\raw\\data";
            Directory.CreateDirectory(DirPath);
            CreateOEITFolderStructure("0.0.2");
        }

        private void CreateOEITFolderStructure(string version)
        {
            FileStream fs=File.Create(DirPath + "\\version.txt");
            System.IO.StreamWriter file = new System.IO.StreamWriter(fs);
            file.WriteLine(version);
            file.Close();

            Directory.CreateDirectory(DirPath + "\\header");
            Directory.CreateDirectory(DirPath+"\\raw");
            Directory.CreateDirectory(DirPath + "\\raw\\data");
            Directory.CreateDirectory(DirPath + "\\raw\\config");
            Directory.CreateDirectory(DirPath + "\\raw\\logs");
            Directory.CreateDirectory(DirPath + "\\eit");
        }
        public void AddConfig(string[] file)
        {
            try
            {
                ConfigPointer++;
                string FileName = DirPath + "\\raw\\config\\config" + ConfigPointer.ToString() + ".txt";
                System.IO.File.WriteAllLines(FileName, file);
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
            }
        }
        private void AddFrame(byte[] data, Int64 TimeStamp, int configPointer)
        {
            fw.Add(data, TimeStamp, configPointer);
        }
        public void AddFrame(byte[] data, Int64 TimeStamp)
        {
            AddFrame(data, TimeStamp, ConfigPointer);
        }
        public void AddLogEntry(string txt)
        {
            DirectoryInfo di = new DirectoryInfo(DirPath);

            if (!di.Exists)
            {
                CreateTempfolder(Name);
            }

            if(!File.Exists(DirPath + "\\raw\\logs\\logs.txt"))
            {
                log = new LogsWR(DirPath + "\\raw\\logs\\logs.txt");
            }
            if (log != null)
            {
                log.Add(txt);
            }
        }
        public void Save()
        {
            Save("oeit");
        }
        public string GetFileName()
        {
            return SavedDirPath + "\\" + Name + Extension;
        }
        public void Save(string dirname)
        {
            try
            {
                string dirnameoeit = dirname;
                DirectoryInfo di = new DirectoryInfo(dirnameoeit);

                if (!di.Exists)
                {
                    Directory.CreateDirectory(dirnameoeit);
                }
                SavedDirPath = dirname;
                ZipFile zip = new ZipFile();
                zip.AddDirectory(DirPath);
                string saveto = dirnameoeit + "\\" + Name + Extension;

                Thread.Sleep(200); // is needed to avoid thead race
                zip.Save(saveto);
                
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.ToString());
            }
        }
    }
}
